###
This is the base class for all widgets. It should not be used directly and it will not work as expected.
You have to create the htmlelem attribute directly in the constructor of your class or in the attributes.

events: [    'button-press', //This event is triggered when a key or mouse button is pressed. The key attribute of the event args is the pressed key
                'button-release', // This event is triggered when a key or mouse button is released
                'focused', // This event is triggered when the widget is focused
                'focus-lost', // This event is triggered when the widget looses focus
                'clicked', //This event is triggered when the user clicks on the widget
                'double-clicked', // This event is triggered when the user double clicks on the widget
                'mouse-move',
                'mouse-in', 
                'mouse-out'],


###

class tree.ui.Widget extends tree.BaseObject
    constructor: () ->
        super
        @active = true
        @hidden = false
        @tooltip = ''
        @border = 0
        @exposed = false
        @htmlelem = null
        @styles = {}
    
    
    ###
    set a style attribute to your widget, the identifier is the css identifier
    ###
    set_style: (identifier, value) ->
        @htmlelem.style[identifier] = value
    
    ###
    set the widgets height in pixel
    ###
    set_height: (height) ->
        if @htmlelem
            @htmlelem.style.height = height + 'px;'
            
        else
            throw new TypeError 'no html elem set'
    
    ###
    set the widgets width in pixel
    ###
    set_width: (width) ->
        if @htmlelem
            @htmlelem.style.width = width + 'px'
            
        else
            throw new TypeError 'no html elem set'
        
    
    ###
    set the width of the border
    it is solid, if you dont want it to be solid, look for tree.ui.Widget.set_style
    ###
    set_border_width: (border) ->
        if @htmlelem
            @htmlelem.style.borderStyle = 'solid'
            @htmlelem.style.borderWidth = border + 'px'
            
        else
            throw new TypeError 'no html elem set'
        
    
    ###
    get the width of the border
    ###
    get_border_width: () ->
        @htmlelem.style.borderWidth
    
    ###
    render the widget and a lot of signals are connected too
    ###
    show: () ->
        @htmlelem.onclick = (evt) =>
            @emit 'clicked', {}
        
        @htmlelem.onkeyup = (evt) =>
            @emit 'button-release', {}
        
        @htmlelem.onkeydown = (evt) => 
            key = String.fromCharCode evt.which
            @emit 'button-press', {key: key}
        
        @htmlelem.onfocus = (evt) =>
            @emit 'focused', {}
        
        @htmlelem.onblur = (evt) =>
            @emit 'focus-lost', {}
        
        @htmlelem.onmouseover = (evt) =>
            @emit 'mouse-in', {}
        
        @htmlelem.onmouseout = (evt) =>
            @emit 'mouse-out', {}
        
        @htmlelem.onmousemove = (evt) =>
            @emit 'mouse-move', {}
        
        @exposed = true
        @htmlelem
    
    ###
    hide the widget
    ###
    hide: () ->
        if @htmlelem
            @htmlelem.style.visibility = 'hidden'
            
        else
            throw new TypeError 'no html elem set'
        
    ###
    show the widget after hiding
    ###
    unhide: () ->
        if @htmlelem
            @htmlelem.style.visibility = 'visible'
            
        else
            throw new TypeError 'no html elem set'
        
    
    activate: () ->
        
    
    is_focus: () ->
        
    
    modify_fg: (color) ->
        if(@htmlelem)
            @htmlelem.style.color = color
            
        else
            throw new TypeError 'no html elem set' 
    
    modify_bg: (color) ->
        if @htmlelem
            @htmlelem.style.backgroundColor = color
            
        else
            throw new TypeError 'no html elem set'
        
    
    set_tooltip: (tooltip) ->
        if @htmlelem
            @htmlelem.title = tooltip
            
        else
            throw new TypeError 'no html elem set'
        
    
    get_tooltp: () ->
        @tooltip
    
    set_geom: (rect) ->
        @set_style 'position', 'absolute'
        @set_style 'left', rect.left() + 'px'
        @set_style 'width', rect.width + 'px'
        @set_style 'top', rect.top() + 'px'
        @set_style 'height', rect.height + 'px'
        @geom = rect
